const install = (Vue, vm) => {
	const isLogin = () => {
		//是否有token
		const token = vm.vuex_token
		if (!token) {
			//来自那个页面
			const currentPage = getCurrentPages().pop()
			//获取页面路径和请求参数
			const {
				options,
				route
			} = currentPage
			const optionsKeys = Object.keys(options)
			let params = ''
			if (optionsKeys.length !== 0) {
				params = optionsKeys.reduce((pre, current) => {
					// return pre + current + '=' + options[current] + '&'
					return `${pre}${current}=${options[current]}&`
				}, '?').slice(0, -1)
			}
			//缓存当前页
			uni.setStorageSync('back_url', route + params)

			vm.$u.toast('请登录')
			setTimeout(() => {
				vm.$u.route({
					type: 'redirect',
					url: 'pages/auth/login',
				})
			}, 1500)
			return false
		}
		return true
	}

	//更新用户信息
	const updataUser = async () => {
		//请求用户信息
		const userInfo = await vm.$u.api.userInfo()

		//缓存用户信息
		vm.$u.vuex('vuex_user', userInfo)
	}
	const isAndroid = () => {
		try {
			const res = uni.getSystemInfo()
			if (res.platform === 'android') return true
				return false

		} catch (e) {
			//TODO handle the exception
		}
	}

	vm.$u.utils = {
		isLogin,
		updataUser,
		isAndroid
	}

}


export default {
	install
}
